/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.mach.service.impl;

import org.omg.CORBA.PUBLIC_MEMBER;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.mach.entity.PurchasedParts;
import org.springblade.mach.entity.StandardParts;
import org.springblade.mach.excel.PurchasedPartsExcel;
import org.springblade.mach.vo.PurchasedPartsVO;
import org.springblade.mach.mapper.PurchasedPartsMapper;
import org.springblade.mach.service.IPurchasedPartsService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 *  服务实现类
 *
 * @author LY
 * @since 2021-04-22
 */
@Service
public class PurchasedPartsServiceImpl extends BaseServiceImpl<PurchasedPartsMapper, PurchasedParts> implements IPurchasedPartsService {

	@Override
	public IPage<PurchasedPartsVO> selectPurchasedPartsPage(IPage<PurchasedPartsVO> page, PurchasedPartsVO purchasedParts) {
		return page.setRecords(baseMapper.selectPurchasedPartsPage(page, purchasedParts));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void importStandardParts(List<PurchasedPartsExcel> data, Boolean isCovered) {
		data.forEach(purchasedParts -> {
			PurchasedParts parts = Objects.requireNonNull(BeanUtil.copy(purchasedParts, PurchasedParts.class));
			if (isCovered) {
				PurchasedParts one = baseMapper.getOneByCodeAndName(parts.getCode(), parts.getName());
				if (one != null && one.getId()!= null) {
					parts.setId(one.getId());
					saveOrUpdate(parts);
					return;
				}
			}
			save(parts);
		});

	}

	@Override
	public PurchasedParts getOneByCodeAndName(String code, String name) {
		return baseMapper.getOneByCodeAndName(code,name);
	}

}
